package com.app.piemoment.entity;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public abstract class PackageCallback {
	public static final String STATUS = "status";
	public static final String MESSAGE = "message";
	public static final String DATA = "data";
	public static final String CODE = "code";

	public String status;
	public int errorCode = -1;
	public String errorMsg;
	public boolean isError = false;

	public PackageCallback parse(JSONObject jsonObject) {
		if (jsonObject == null) {
			isError = true;
			errorMsg = "获取数据失败，请检查网络。";
			return this;
		}

		Log.e("callback", jsonObject.toString());
		try {
			status = jsonObject.getString(STATUS);

			if (status.equals("error")) {
				isError = true;
				errorMsg = jsonObject.getString(MESSAGE);
				return this;
			} else {
				isError = false;
				JSONObject datajJsonObject = jsonObject.getJSONObject("data");
				return parseItem(datajJsonObject);

			}

		} catch (JSONException e) {
			isError = true;
		}
		return this;
	};

	public abstract PackageCallback parseItem(JSONObject jsonObject);

	public int getErorrCode() {
		return errorCode;
	}

	public String getErrorMsg() {
		return errorMsg;
	}
}
